﻿using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyTiemVang.BUS
{
    class ReportBUS
    {
        public static void PhieuBanHangReport(int maPhieuBanHang, string path)
        {
            // Setup DataSet
            QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();
            //Dataset ds = from d in db.g

            var ds = from d in db.GetPhieuBanHang(maPhieuBanHang)
                     select d;
            // Create Report DataSource
            ReportDataSource rds = new ReportDataSource("DSPhieuBanHang", ds);


            // Variables
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;


            // Setup the report viewer object and get the array of bytes
            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Local;

            //string startupPath = Environment.CurrentDirectory;
            //string realPath = startupPath.Substring(0, startupPath.IndexOf("bin")) + "Reports\\RPPhieuBanHang.rdlc";
            string startupPath = Application.StartupPath;
            string realPath = Path.Combine(startupPath, "Reports", "RPPhieuBanHang.rdlc");
            viewer.LocalReport.ReportPath = realPath;
            viewer.LocalReport.DataSources.Add(rds); // Add datasource here


            byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType,
                out encoding, out extension, out streamIds, out warnings);

            //create file stream in create mode
            FileStream fs = new FileStream(path, FileMode.Create);
            //create Excel file
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
            System.Diagnostics.Process.Start(path);
        }

        public static void PhieuNoReport(int maPhieuNo, string path)
        {
            // Setup DataSet
            QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();
            //Dataset ds = from d in db.g

            var ds = from d in db.GetPhieuNo(maPhieuNo)
                     select d;
            // Create Report DataSource
            ReportDataSource rds = new ReportDataSource("DSPhieuNo", ds);


            // Variables
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;


            // Setup the report viewer object and get the array of bytes
            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Local;

            //string startupPath = Environment.CurrentDirectory;
            //string realPath = startupPath.Substring(0, startupPath.IndexOf("bin")) + "Reports\\RPPhieuNo.rdlc";
            string startupPath = Application.StartupPath;
            string realPath = Path.Combine(startupPath, "Reports", "RPPhieuNo.rdlc");
            viewer.LocalReport.ReportPath = realPath;
            viewer.LocalReport.DataSources.Add(rds); // Add datasource here


            byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType,
                out encoding, out extension, out streamIds, out warnings);

            //create file stream in create mode
            FileStream fs = new FileStream(path, FileMode.Create);
            //create Excel file
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
            System.Diagnostics.Process.Start(path);
        }

        public static void PhieuDichVuReport(int maPhieuDichVu, string path)
        {
            // Setup DataSet
            QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();
            //Dataset ds = from d in db.g

            var ds = from d in db.GetPhieuDichVu(maPhieuDichVu)
                     select d;
            // Create Report DataSource
            ReportDataSource rds = new ReportDataSource("DSPhieuDichVu", ds);


            // Variables
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;

            // Setup the report viewer object and get the array of bytes
            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Local;

            //string startupPath = Environment.CurrentDirectory;
            //string realPath = startupPath.Substring(0, startupPath.IndexOf("bin")) + "Reports\\RPPhieuDichVu.rdlc";
            string startupPath = Application.StartupPath;
            string realPath = Path.Combine(startupPath, "Reports", "RPPhieuDichVu.rdlc");
            viewer.LocalReport.ReportPath = realPath;
            viewer.LocalReport.DataSources.Add(rds); // Add datasource here


            byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType,
                out encoding, out extension, out streamIds, out warnings);

            //create file stream in create mode
            FileStream fs = new FileStream(path, FileMode.Create);
            //create Excel file
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
            System.Diagnostics.Process.Start(path);
        }

        public static void PhieuMuaHangReport(int maPhieuMuaHang, string path)
        {
            // Setup DataSet
            QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();
            //Dataset ds = from d in db.g

            var ds = from d in db.GetPhieuMuaHang(maPhieuMuaHang)
                     select d;
            // Create Report DataSource
            ReportDataSource rds = new ReportDataSource("DSPhieuMuaHang", ds);

            // Variables
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;

            // Setup the report viewer object and get the array of bytes
            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Local;

            //string startupPath = Environment.CurrentDirectory;
            //string realPath = startupPath.Substring(0, startupPath.IndexOf("bin")) + "Reports\\RPPhieuMuaHang.rdlc";
            string startupPath = Application.StartupPath;
            string realPath = Path.Combine(startupPath, "Reports", "RPPhieuMuaHang.rdlc");
            viewer.LocalReport.ReportPath = realPath;
            viewer.LocalReport.DataSources.Add(rds); // Add datasource here


            byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType,
                out encoding, out extension, out streamIds, out warnings);

            //create file stream in create mode
            FileStream fs = new FileStream(path, FileMode.Create);
            //create Excel file
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
            System.Diagnostics.Process.Start(path);
        }

        public static void PhieuHangGiacongReport(int maPhieuHangGC, string path)
        {
            // Setup DataSet
            QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();
            //Dataset ds = from d in db.g

            var ds = from d in db.GetPhieuHangGiaCong(maPhieuHangGC)
                     select d;
            // Create Report DataSource
            ReportDataSource rds = new ReportDataSource("DSPhieuHangGiaCong", ds);


            // Variables
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;

            // Setup the report viewer object and get the array of bytes
            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Local;

            //string startupPath = Environment.CurrentDirectory;
            //string realPath = startupPath.Substring(0, startupPath.IndexOf("bin")) + "Reports\\RPPhieuHangGiaCong.rdlc";
            string startupPath = Application.StartupPath;
            string realPath = Path.Combine(startupPath, "Reports", "RPPhieuHangGiaCong.rdlc");
            viewer.LocalReport.ReportPath = realPath;
            viewer.LocalReport.DataSources.Add(rds); // Add datasource here


            byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType,
                out encoding, out extension, out streamIds, out warnings);

            //create file stream in create mode
            FileStream fs = new FileStream(path, FileMode.Create);
            //create Excel file
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
            System.Diagnostics.Process.Start(path);
        }

        public static void PhieuChiReport(int maPhieuChi, string path)
        {
            // Setup DataSet
            QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();
            //Dataset ds = from d in db.g

            var ds = from d in db.GetPhieuChi(maPhieuChi)
                     select d;
            // Create Report DataSource
            ReportDataSource rds = new ReportDataSource("DSPhieuChi", ds);


            // Variables
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;

            // Setup the report viewer object and get the array of bytes
            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Local;

            //string startupPath = Environment.CurrentDirectory;
            //string realPath = startupPath.Substring(0, startupPath.IndexOf("bin")) + "Reports\\RPPhieuChi.rdlc";
            string startupPath = Application.StartupPath;
            string realPath = Path.Combine(startupPath, "Reports", "RPPhieuChi.rdlc");
            viewer.LocalReport.ReportPath = realPath;
            viewer.LocalReport.DataSources.Add(rds); // Add datasource here


            byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType,
                out encoding, out extension, out streamIds, out warnings);

            //create file stream in create mode
            FileStream fs = new FileStream(path, FileMode.Create);
            //create Excel file
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
            System.Diagnostics.Process.Start(path);
        }

        public static void TonKhoReport(int maBaoCao, string path)
        {
            // Setup DataSet
            QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();
            //Dataset ds = from d in db.g

            var ds = from d in db.GetBaocaoTonKho(maBaoCao)
                     select d;
            // Create Report DataSource
            ReportDataSource rds = new ReportDataSource("DSBaoCaoTonKho", ds);


            // Variables
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;


            // Setup the report viewer object and get the array of bytes
            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Local;

            //string startupPath = Environment.CurrentDirectory;
            //string realPath = startupPath.Substring(0, startupPath.IndexOf("bin")) + "Reports\\RPBaoCaoTonKho.rdlc";
            string startupPath = Application.StartupPath;
            string realPath = Path.Combine(startupPath, "Reports", "RPBaoCaoTonKho.rdlc");
            viewer.LocalReport.ReportPath = realPath;
            viewer.LocalReport.DataSources.Add(rds); // Add datasource here


            byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType,
                out encoding, out extension, out streamIds, out warnings);

            //create file stream in create mode
            FileStream fs = new FileStream(path, FileMode.Create);
            //create Excel file
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
            System.Diagnostics.Process.Start(path);
        }

        public static void PhieuNhapKhoReport(int maPhieuNhapKho, string path)
        {
            // Setup DataSet
            QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();
            //Dataset ds = from d in db.g

            var ds = from d in db.GetPhieuNhapKho(maPhieuNhapKho)
                     select d;
            // Create Report DataSource
            ReportDataSource rds = new ReportDataSource("DSPhieuNhapKho", ds);


            // Variables
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;

            // Setup the report viewer object and get the array of bytes
            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Local;

            //string startupPath = Environment.CurrentDirectory;
            //string realPath = startupPath.Substring(0, startupPath.IndexOf("bin")) + "Reports\\RPPhieuNhapKho.rdlc";
            string startupPath = Application.StartupPath;
            string realPath = Path.Combine(startupPath, "Reports", "RPPhieuNhapKho.rdlc");
            viewer.LocalReport.ReportPath = realPath;
            viewer.LocalReport.DataSources.Add(rds); // Add datasource here


            byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType,
                out encoding, out extension, out streamIds, out warnings);

            //create file stream in create mode
            FileStream fs = new FileStream(path, FileMode.Create);
            //create Excel file
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
            System.Diagnostics.Process.Start(path);
        }
    }
}
